package cn.stimd.spring.beans.factory.annotation;

import java.beans.PropertyDescriptor;
import java.lang.reflect.Member;
import java.util.Collection;

/**
 * 依赖注入的元数据
 * <p>
 * Created by Stimd
 * 项目地址：https://gitee.com/stimd/spring-wheel
 * 公众号：Java编程探微
 */
public class InjectionMetadata {
    private final Class<?> targetClass;
    private final Collection<InjectedElement> injectedElements;

    public InjectionMetadata(Class<?> targetClass, Collection<InjectedElement> injectedElements) {
        this.targetClass = targetClass;
        this.injectedElements = injectedElements;
    }


    /**
     * 遍历所有的InjectMetadata，具体的注入由子类完成
     */
    public void inject(Object target, String beanName) throws Throwable {
        for (InjectedElement element : this.injectedElements) {
            element.inject(target, beanName);
        }
    }

    public Class<?> getTargetClass() {
        return targetClass;
    }


    public static abstract class InjectedElement{
        protected final Member member;
        protected final PropertyDescriptor pd;

        public InjectedElement(Member member, PropertyDescriptor pd) {
            this.member = member;
            this.pd = pd;
        }

        protected abstract void inject(Object bean, String beanName) throws Throwable;
    }
}
